package com.lechi.iot.netty.message;

import com.lechi.iot.netty.data.TcpData;
import com.lechi.iot.netty.utils.IntegerToByteUtil;
import org.apache.commons.lang3.ArrayUtils;

public class TcpByteArrayMessage implements TcpMessage {

    private String deviceGroup;
    private byte[] bytes;
    private TcpData data;

    public TcpByteArrayMessage(byte[] bytes) {
        this.bytes = bytes;
    }

    @Override
    public String deviceGroup() {
        return this.deviceGroup;
    }

    @Override
    public int length() {
        byte[] dataLength = ArrayUtils.subarray(bytes, DATA_INDEX, COMMAND_INDEX);
        return IntegerToByteUtil.bytesToInt(dataLength);
    }

    @Override
    public int adviceAddr() {
        return bytes[COMMAND_INDEX] & 0x7F;
    }

    @Override
    public TcpData data() {
        return this.data;
    }

    @Override
    public byte[] toBytes() {
        return bytes;
    }

    @Override
    public String deviceSn() {
        return null;
    }


}
